<template>
  <div class="login">
    <Header title="登录"/>
    <div class="img">美团</div>
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
          v-model="username"
          name="user"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="pass"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <div style="margin: 16px; margin-top: 60px">
        <van-button round block type="primary" native-type="submit">
          登录
        </van-button>
      </div>
      <div style="margin: 16px; margin-top: 60px" @click="register">
        <van-button round block type="primary" native-type="submit">
          去注册
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import Header from "@/components/Header.vue";
import { reactive, toRefs } from "vue";
import {useRouter} from 'vue-router'
import { showToast } from 'vant';
import 'vant/es/toast/style';
export default {
  components: { Header },
  setup() {
    const router = useRouter()
    const data = reactive({
      username: "",
      password: "",
    });
    const onSubmit = (values) => {
      // console.log("submit", values);
      const token = JSON.parse(localStorage.userInfo)
      if(!localStorage.userInfo){
        showToast('账号未注册！')
        return;
      }else{
        if(values['user'] === token['user']){
         if(values['pass'] === token['pass']){
           router.push('/home');
           localStorage.setItem('isLogin','token')
           showToast('登录成功')
         }else{
            showToast('密码错误')
         }
        }else{
          showToast('账号不存在！')
        }
      }
    };
    const register = ()=>{
        router.push('/register')
    }
    return {
      ...toRefs(data),
      onSubmit,
      register
    };
  },
};
</script>

<style lang="less" scoped>
.login {
  .img {
    width: 200px;
    height: 200px;
    background-color: #ffc400;
    font-size: 80px;
    line-height: 200px;
    text-align: center;
    border-radius: 40px;
    margin: 20px auto;
  }
  .register {
    margin-top: 20px;
  }
  /deep/ .van-button--primary {
    background-color: #ffc400;
    border: none;
  }
}
</style>